﻿//+-------------------------------------------------------------------------------------------------------------------------------------------------------
//
//Description: SqlProfileViewModel
//Author: Aravind Ramarathinam (aravr@microsoft.com)
//
//Copyright © Microsoft Corporation
//Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
//You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
//
//THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
//INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. 
//See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.
// 
//--------------------------------------------------------------------------------------------------------------------------------------------------------

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using VmroleAuthor.Models;


namespace VmroleAuthor.ViewModels
{   
    public class SqlProfileViewModel : TreeViewItemViewModel, IAddSqlDeployment, IDataErrorInfo, IDataWarningInfo, IProduceValidationResults, IRemoveChildItem, ICanBeRemovedFromTree
    {
        private readonly VmRoleResourceExtensionViewModel vmRoleResourceExtensionViewModel;
        private readonly SQLProfile sqlProfile;

        public static class PropertyNames
        {
            public const string Summary = "Summary";       
        }

        public SqlProfileViewModel(
            SQLProfile sqlProfile,
            ApplicationProfileViewModel applicationProfileViewModel,
            VmRoleResourceExtensionViewModel resourceExtensionViewModel)
            : base(applicationProfileViewModel, true)
        {
            if (sqlProfile == null)
            {
                sqlProfile = new SQLProfile();
            }
            this.sqlProfile = sqlProfile;
            this.vmRoleResourceExtensionViewModel = resourceExtensionViewModel;
            this.ValidationResults = new ObservableCollection<CloudServiceModelValidationResult>();   
        }

        public SQLProfile SQLProfile
        {
            get { return this.sqlProfile; }
        }

        public string Summary
        {
            get
            {
                if (this.sqlProfile.SQLDeployments == null)
                {
                    return "SQL Profile";
                }
                return "SQL Profile (" + this.sqlProfile.SQLDeployments.Count() + ")";
            }
        }     

        public bool CanAddSqlDeployment
        {
            get { return true; }
        }

        public void AddSqlDeployment()
        {
            this.IsExpanded = true;
            if (this.sqlProfile.SQLDeployments == null)
            {
                this.sqlProfile.SQLDeployments = new List<SQLDeployment>();
            }
            SQLDeployment sqlDeployment = new SQLDeployment();
            this.sqlProfile.SQLDeployments.Add(sqlDeployment);
            var sqlDeploymentViewModel = new SqlDeploymentViewModel(sqlDeployment, this, this.vmRoleResourceExtensionViewModel);
            this.Children.Add(sqlDeploymentViewModel);
            sqlDeploymentViewModel.IsSelected = true;
        }

      
        protected override void LoadChildren()
        {
            if (this.sqlProfile.SQLDeployments != null)
            {
                foreach (SQLDeployment dep in this.sqlProfile.SQLDeployments)
                {
                    this.Children.Add(new SqlDeploymentViewModel(dep, this, this.vmRoleResourceExtensionViewModel));
                }
            }
        }

        public void RemoveChild(TreeViewItemViewModel childItem)
        {
            var sqlDeploymentViewModel = childItem as SqlDeploymentViewModel;
            if (sqlDeploymentViewModel != null)
            {
                sqlDeploymentViewModel.Cleanup();
                this.sqlProfile.SQLDeployments.Remove(sqlDeploymentViewModel.SQLDeployment);
                this.Children.Remove(childItem);
            }
        }

        public void Cleanup()
        {
            this.IsExpanded = true;
            ParametersAndPayloadCleanupHelper.CleanupParametersAndPayload(this, typeof(PropertyNames));
        }

        public string this[string propertyName]
        {
            get
            {
                return null;
            }
        }

        public string Error { get; set; }

        public ObservableCollection<CloudServiceModelValidationResult> ValidationResults { get; private set; }

        public void UpdateValidationResults()
        {
            this.IsExpanded = true;
            ModelValidationResultsHelper.UpdateValidationResults(this, typeof(PropertyNames));
            //Add any other cross-field validations error            
        }

        public string GetWarning(string propertyName)
        {
            if (this.sqlProfile == null ||
                this.sqlProfile.SQLDeployments == null ||
                this.sqlProfile.SQLDeployments.Count == 0)
            {
                return "Add atleast one SQL Deployment to SQL profile";
            }
            return null;
        }
    }
}
